﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

using AutoMapper;

using Happy.Archetype;
using Happy.Application.Domain;
using Happy.Security.Domain;
using Happy.Security.Domain.Users;
using Happy.Security.Query.Users;

namespace Happy.Security.Application.Users
{
    [ApplicationService]
    public sealed class UserService : AggregateRootService<User, UserView, ISecurityUnitOfWork>
    {
        protected override User CreateAggregate(UserView viewModel)
        {
            var aggregate = this.Service<UserFactory>()
                .Create(viewModel.UserName, viewModel.PlantextPassword);

            return Mapper.Map(viewModel, aggregate);
        }

        protected override void UpdateAggregate(User aggregate, UserView viewModel)
        {
            this.Service<PasswordService>().ChangePassword(aggregate, viewModel.PlantextPassword);
            Mapper.Map(viewModel, aggregate);
        }
    }
}
